const app = getApp()
Page({

  data: {
      city:'柳州',
      flag:false,
      weather:{},
      name:'',
      daily:'',//天气指数
      hour:[],//存储8个时间段的天气信息
      days:'15d',
      daysday:[],//存储n天天气信息

  },
  // 获取输入框值
  getInput(e){
    console.log(e)
    this.setData({
      city:e.detail.value
    })
  },
  //点击搜索按钮，查询请求
getsearch(){
  this.get_weather()
},
//获取实时天气
get_weather(){
  //获取全球id
  wx.request({
    url: 'https://m336x76v5r.re.qweatherapi.com/geo/v2/city/lookup',
    data:{
      key:app.globalData.key,
      location:this.data.city
    },
    success:(res)=>{
      console.log(res.data)
      this.setData({
        location:res.data.location[0].id,
        name:res.data.location[0].name
      })
      //获取上方locationId然后查询实时天气
        var location_id = this.data.location;
       //获取实时天气
        wx.request({
          url: 'https://m336x76v5r.re.qweatherapi.com/v7/weather/now',
          method:"GET",
          data:{
            key:app.globalData.key,
            location:location_id
          },
          success:(res)=>{
            console.log(res)
            this.setData({
              weather:res.data.now,
              flag:true
            })
          }
        });
        // 获取天气指数
        wx.request({
          url:'https://m336x76v5r.re.qweatherapi.com/v7/indices/1d',
          data:{
            key:app.globalData.key,
            location:location_id,
            type:3
          },
          success:(res)=>{
            console.log(res)
            this.setData({
              daily:res.data.daily,
              flag:true

            })
          }
        });
        //获取24小时天气
      wx.request({
        url:'https://m336x76v5r.re.qweatherapi.com/v7/weather/24h',
        data:{
          key:app.globalData.key,
          location:location_id,
        },
        success:(res)=>{
          console.log(res)
          //获取api返回的天气
          let weatherData = res.data;
          //提取2前24个时间段的天气 slice：提取数组指定范围
          let hourweather = weatherData.hourly.slice(0,24)
          console.log(hourweather)

          
          //map：遍历数组
          let time = hourweather.map(item=>{
            //fxTime：api的时间名称；substring：截取时间的小时和分钟（从左到右数0开始）
            let times = item.fxTime.substring(11,16);
            return times;
          })
          this.setData({
            hour:hourweather,
            flag:true,
            times:time//更新截取时间
          })
        }
      });
      //获取每日天气
      wx.request({
        url: 'https://m336x76v5r.re.qweatherapi.com/v7/weather/7d',
        data:{
          key:app.globalData.key,
          location:location_id,
        },
        success:(res)=>{
          console.log(res)
          let fxDates = res.data.daily.map(item=>{
          let fxDate = item.fxDate.substring(5,10);
          return fxDate;
          })
          this.setData({
            daysday:res.data.daily,
            flag:true,
            fxDate:fxDates
          })
        }
      })
      },
  })
},
onLoad(option){
  this.get_weather()
}
})